package com.leen.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leen.common.vo.ResponseVO;
import com.leen.common.vo.ResultEnum;
import com.leen.common.vo.ResultVO;
import com.leen.news.dto.YxSystemConfigDTO;
import com.leen.news.entity.YxSystemConfig;
import com.leen.news.mapper.YxSystemConfigMapper;
import com.leen.news.service.YxSystemConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class YxSystemConfigServiceImpl implements YxSystemConfigService {
    @Autowired
    private YxSystemConfigMapper yxSystemConfigMapper;
    @Override
    public ResponseVO search(YxSystemConfigDTO yxSystemConfigDTO) {
        IPage<YxSystemConfig> page = new Page<>(yxSystemConfigDTO.getPageNumber(), yxSystemConfigDTO.getPageSize());
        QueryWrapper<YxSystemConfig> queryWrapper=new QueryWrapper<>();
//        if (StringUtils.isNotBlank(yxSystemConfigDTO.getMenuName())){
//            queryWrapper.like("menu_name",yxSystemConfigDTO.getMenuName());
//        }
        IPage<YxSystemConfig> page1 = this.yxSystemConfigMapper.selectPage(page, queryWrapper);
        return new ResponseVO(page1.getTotal(),page1.getRecords());
    }

    @Override
    public ResultVO<String> insert(YxSystemConfig entity) {
        int insert = this.yxSystemConfigMapper.insert(entity);
        return insert > 0 ? ResultVO.success(ResultEnum.DB_SUCCESS.getMessage()) : ResultVO.failed(ResultEnum.DB_FAILED);
    }

    @Override
    public ResultVO<String> updateById(YxSystemConfig entity) {
        int update = this.yxSystemConfigMapper.updateById(entity);
        return update > 0 ? ResultVO.success(ResultEnum.DB_SUCCESS.getMessage()) : ResultVO.failed(ResultEnum.DB_FAILED);
    }

    @Override
    public ResultVO<YxSystemConfig> selectById(Serializable id) {
        YxSystemConfig yxSystemConfig = this.yxSystemConfigMapper.selectById(id);
        return ResultVO.success(yxSystemConfig);
    }

    @Override
    public List<YxSystemConfig> findAll() {
        QueryWrapper<YxSystemConfig> queryWrapper = new QueryWrapper<>();
        return this.yxSystemConfigMapper.selectList(queryWrapper);
    }
}
